import { stringify } from 'qs';
import request from '@utils/request';

export default {
  query({ categoryId, title, abstract, status, page = 0, size = 10 }) {
    return request(
      `/api/cms/article?${stringify({ categoryId, title, abstract, status, page, size })}`
    );
  },

  info({ id }) {
    return request(`/api/cms/article/${id}`);
  },

  add({ categoryId, title, cover, abstract, content, keywords, tags, source, url, status }) {
    return request('/api/cms/article', {
      method: 'POST',
      body: {
        categoryId,
        title,
        cover,
        abstract,
        content,
        keywords,
        tags,
        source,
        url,
        status,
      },
    });
  },

  update({ id, categoryId, title, cover, abstract, content, keywords, tags, source, url, status }) {
    return request(`/api/cms/article/${id}`, {
      method: 'PUT',
      body: {
        id,
        categoryId,
        title,
        cover,
        abstract,
        content,
        keywords,
        tags,
        source,
        url,
        status,
      },
    });
  },

  remove({ ids }) {
    const result = Promise.all(
      ids.map(id => {
        return request(`/api/cms/article/${id}`, {
          method: 'DELETE',
        });
      })
    );

    return result
      .then(() => {
        return '删除成功';
      })
      .catch(e => {
        return e.message || '删除失败';
      });
  },
};
